﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Contracts.Exceptions;

namespace XcoAppSpaces.Transport.MSMQ
{
	/// <summary>
    /// Configuration string interpreter for the XcoMSMQTransportService.
	/// </summary>
    public class ConfigStringInterpreter : XcoConfigStringInterpreterBase
	{
		/// <summary>
		/// Config key for setting the queue name.
		/// </summary>
        public const string ConfigQueueName = "msmq.queuename";		
		/// <summary>
		/// Configuration key for setting the name.
		/// </summary>
		public const string ConfigName = "msmq.name";
		/// <summary>
		/// Configuration key for setting if the service is the default communication service.
		/// </summary>
		public const string ConfigIsDefault = "msmq.isdefault";
		/// <summary>
		/// Remove queue after using
		/// </summary>
		public const string ConfigDisposeQueue = "msmq.disposequeue";


        private bool? _removeQueueOnStop;
        private string _serviceName;
        private string _queueName;
        private bool? _isdefault;

        /// <summary>
        /// Action definition for interpretion
        /// </summary>
        public override Dictionary<string, Action<string>> InterpretDefinition
        {
            get
            {
                return new Dictionary<string, Action<string>> 
                    { 
                        {ConfigQueueName, AnalyseQueueName},//Configuration key for setting the queue name.
                        {ConfigDisposeQueue, AnalyseDisposeQueue},//Configuration key for removing queue after using
                        {ConfigName, AnalyseName},//Configuration key for setting the name.
                        {ConfigIsDefault, AnalyseIsDefault},//Configuration key for setting if the service is the default communication service.
                    };
            }
        }

        /// <summary>
        /// Get the current tcp service
        /// </summary>
        public override IXcoService GetService
        {
            get
            {
                if (_removeQueueOnStop.HasValue || _queueName != null || _serviceName != null || _isdefault.HasValue)
                {
                    if (_serviceName != null)
                    {
                        ServiceConfig.Name = _serviceName;
                    }
                    if (_isdefault.HasValue)
                    {
                        ServiceConfig.IsDefault = _isdefault.Value;
                    }
                    return _removeQueueOnStop.HasValue
                               ? new XcoMSMQTransportService(_queueName, _removeQueueOnStop.Value)
                               : new XcoMSMQTransportService(_queueName);
                }
                return null;
            }
        }

        #region analyse methods


        void AnalyseQueueName(string queueName)
        {
            _queueName = queueName;
        }
        void AnalyseDisposeQueue(string disposeQueueStr)
        {
            bool removeQueueOnStop;
            if (bool.TryParse(disposeQueueStr, out removeQueueOnStop))
            {                //try to load the isDefault-definition from the configuration
                _removeQueueOnStop = removeQueueOnStop;
            }
            else
            {
                throw new XcoConfigurationException("DisposeOnStop must be either \"true\" or \"false\"!");
            }
        }

        void AnalyseName(string name)
        {
            _serviceName = name;
        }

        void AnalyseIsDefault(string isDefaultStr)
        {
            bool isDefault;
            if (bool.TryParse(isDefaultStr, out isDefault))
            {                //try to load the isDefault-definition from the configuration
                _isdefault = isDefault;
            }
            else
            {
                throw new XcoConfigurationException("IsDefault must be either \"true\" or \"false\"!");
            }
        }
        #endregion	
		
	}
}
